//
//  StartViewController.swift
//  paomadeng
//
//  Created by 初光夫 on 2021/11/17.
//

import UIKit

class StartViewController: UIViewController {
    
    var bgColor: UIColor?
    var cusTitle = ""
    var cusTitleColor: UIColor?
    var cusFont = UIFont.systemFont(ofSize: 80)
    var duration = 8
    var fontSize = UIScreen.main.bounds.size.width - 20
    
    private let showTitle = UILabel()
    
    let swidth = UIScreen.main.bounds.size.width
    let sheight = UIScreen.main.bounds.size.height

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = bgColor
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapClick))
        tap.numberOfTapsRequired = 2
        self.view.addGestureRecognizer(tap)
        
        showTitle.frame = CGRect.init(x: 0, y: 0, width: 100, height: 50)
        showTitle.text = cusTitle
        showTitle.backgroundColor = .clear
        showTitle.transform = CGAffineTransform(rotationAngle: .pi / 2)
        showTitle.text = cusTitle
        showTitle.textColor = cusTitleColor ?? .white
        showTitle.font = UIFont.systemFont(ofSize: fontSize)
        view.addSubview(showTitle)
        
        let str: NSString = cusTitle as NSString
        let realidth = str.boundingRect(with: CGSize.init(width: CGFloat(HUGE), height: sheight), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font : showTitle.font!], context: nil).size.width
        
        
        showTitle.frame = CGRect.init(x: 0, y: sheight, width: swidth, height: realidth)
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
            let animation = CABasicAnimation.init(keyPath: "transform.translation.y")
            //animation.fromValue = 0
            animation.toValue = -self.sheight - realidth
            animation.duration = CFTimeInterval(self.duration)
            animation.isRemovedOnCompletion = true
            animation.repeatCount = MAXFLOAT
            animation.fillMode = .forwards
            self.showTitle.layer.add(animation, forKey: nil)
        }
        
    }
    
    @objc func tapClick() {
        self.dismiss(animated: true, completion: nil)
    }

    deinit {
        print("start释放")
    }
}
